//
//  ZJOSCourseViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/20.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJOSCourseViewController.h"
#import "ActivityJoinTableViewCell.h"
#import "ZJRefurbish.h"
#import "ZJCommunitySchoolModel.h"
#import "LPActivityDetailViewController.h"

#define CELLID @"ActivityJoinTableViewCell"
@interface ZJOSCourseViewController ()<UITableViewDataSource,UITableViewDelegate,ActivityJoinCellDelegate,LPCostomHeadLineAndTextViewAndTiShiDelegate>
{
   
    ZJCommunitySchoolModel *_communitymodel;
    NSInteger _tisIndex;/**标记提示的下标 */
}
/** tableview */
@property (nonatomic, strong) UITableView *tableView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 刷新 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  jc  */
@property (strong, nonatomic) JCAlertView  *alertView;
/** 提示model */
@property (nonatomic, strong) ActivityJoinDetailModel *tishiModel;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation ZJOSCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.height = self.view.height - LP_TopHeight - 44 - 268 * [UIScreen zj_screenHScale];
    [self.view addSubview:self.tableView];
    _communitymodel = [ZJCommunitySchoolModel yy_modelWithJSON:self.dataDic];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self.tableView.mj_header beginRefreshing];
}



#pragma mark - 网络请求
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"PSID":_communitymodel.ID,@"PAGE": @"1",
                           @"COUNT":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getByPSID] Parameter:dict Success:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON: object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:[object objectForKey:@"ACT_LIST"]] mutableCopy];
            [weakSelf.tableView reloadData];
        }else{
            [YJProgressHUD showMessage:@"请求错误" inView:weakSelf.view];
        }
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showMessage:@"请求错误" inView:weakSelf.view];
    }];
}

- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"PSID":_communitymodel.ID,@"PAGE": [NSString stringWithFormat:@"%ld",_page],
                           @"COUNT":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getByPSID] Parameter:dict Success:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON: object];
        if ([model.STATE isEqualToString:@"20000"]) {
             NSArray *array = [NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:[object objectForKey:@"ACT_LIST"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }else{
            [YJProgressHUD showMessage:@"请求错误" inView:weakSelf.view];
        }
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showMessage:@"请求错误" inView:weakSelf.view];
    }];
}


#pragma mark - ActivityJoinCellDelegate 协议方法
- (void)ActivityJoinClick_index:(NSInteger)index section:(NSInteger)section statusText:(NSString *)statusText{
    __weak typeof(self) weakSelf = self;
    ActivityJoinDetailModel *model = self.dataArray[index];
    if ([statusText isEqualToString:@"加入"]) {
        [YJProgressHUD show:@"报名中..." inView:self.view mode:YJProgressModeLoading];
        /**  这是点击加入  */
        NSDictionary *paramer = @{@"AI_ID": model.ID,
                                  @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                  @"USER_NAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME};
        [NetWorkingManager postWithUrlString:[NetWorkingURL getactactionMobileEnroll] Parameter:paramer Success:^(id object) {
             NSString *CODE = [object objectForKey:@"CODE"];
            if ([CODE isEqualToString:@"20000"]) {
                [MobClick event:[UMMStatisticsString registerAct]];
                [YJProgressHUD showMsgWithoutView:@"报名成功"];
                [weakSelf.tableView.mj_header beginRefreshing];
                
            }else if ([CODE isEqualToString:@"80001"]) {
                [YJProgressHUD showMsgWithoutView:@"重复报名"];
            }else if ([CODE isEqualToString:@"80002"]) {
                [YJProgressHUD showMsgWithoutView:@"活动结束"];
            }else if ([CODE isEqualToString:@"80003"]) {
                [YJProgressHUD showMsgWithoutView:@"人数达上限"];
            }else if ([CODE isEqualToString:@"80004"]) {
                [YJProgressHUD showMsgWithoutView:@"无此活动或已被删除!"];
            }else{
                [YJProgressHUD showMsgWithoutView:@"报名失败"];
            }
            
        } Fail:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }];
    }else if([statusText isEqualToString:@"请假"]){
        
        LPCostomHeadLineAndTextViewAndTiShiView *tishi = [[LPCostomHeadLineAndTextViewAndTiShiView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, [UIScreen zj_screenHeight]) headLine:@"请输入原因"];
        tishi.delegate = self;
        self.alertView = [[JCAlertView alloc] initWithCustomView:tishi dismissWhenTouchedBackground:YES];
        [self.alertView show];
        _tisIndex = index;
        self.tishiModel = model;
    }
}
#pragma mark - LPCostomHeadLineAndTextViewAndTiShiDelegate
- (void)LPCostomHeadLineAndTextViewAndTiShiWithCancle:(BOOL)cancle{
    [self.alertView dismissWithCompletion:nil];
}

- (void)LPCostomHeadLineAndTextViewAndTiShiWithString:(NSString *)string{
    __weak typeof(self) weakSelf = self;
    /**   这里是请假  */
    [YJProgressHUD show:@"请假中..." inView:self.view mode:YJProgressModeLoading];
    NSDictionary *paramer = @{@"AI_ID": self.tishiModel.ID,
                              @"FROM_USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"FROM_USERNAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,
                              @"FROM_UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"PMID": ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID,
                              @"CONTENT": string,
                              @"TO_UNITID": self.tishiModel.UNITID,
                              @"FROM_UNITNAME": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.DEPT_NAME,
                              @"ACT_NAME": self.tishiModel.NAME};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActLeave] Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            [YJProgressHUD showMsgWithoutView:@"请假成功"];
            /**  请假成功修改数据源刷新界面  */
            weakSelf.tishiModel.IS_LEAVE = @"1";
            weakSelf.tishiModel.LEAVE_STATE = @"0";
            weakSelf.dataArray[_tisIndex] = weakSelf.tishiModel;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_tisIndex inSection:0];
            [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }else{
            [YJProgressHUD showMsgWithoutView:@"请假失败"];
        }
        
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"请假失败"];
    }];
    
    [self.alertView dismissWithCompletion:nil];
    
}

#pragma mark - UITabelView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityJoinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.index = indexPath.row;
    cell.section = indexPath.section;
    cell.isTitleTwo = YES;
    cell.model = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115 * [UIScreen zj_screenHScale];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    LPActivityDetailViewController *vc = [LPActivityDetailViewController new];
    vc.model = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter


#pragma mark - Getters

- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ActivityJoinTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
